package com.levi.completableFuture;

import java.util.concurrent.*;

/**
 * @author shentong
 * @since 2022/1/16 4:42 PM
 * 使用handler()方法，执行下一个异步任务  handler方法可以对异常进行处理
 */
public class HandleDemo {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                int i = 1/0;
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
            return 123;
        }, executorService).handleAsync((integer, throwable) -> {
            if (throwable == null) {
                return integer * 10;
            }
            System.out.println("出现异常了～"+Thread.currentThread().getName());
            return null;
        },executorService);
        System.out.println("Main End~");
        try {
            Integer integer = integerCompletableFuture.get();
            System.out.println(integer);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
    }
}
